package com.ybwh.springboot2.mapstruct;

import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

/**
 * <p>
 *
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2022/3/13 11:16
 */
@Mapper(componentModel = "default",uses = {MyConvert.class}, unmappedSourcePolicy = ReportingPolicy.WARN, unmappedTargetPolicy = ReportingPolicy.WARN)
public interface PersonMapstruct {

    /**
     * 用来调用实例 实际开发中可使用注入Spring  不写
     */
    PersonMapstruct INSTANCE = Mappers.getMapper(PersonMapstruct.class);


    //    @Mapping(target = "posName", source = "pos.name")
//    @Mapping(target = "posName", source = "pos", qualifiedByName = "myTransformation")
    @Mapping(target = "posName", source = "pos", qualifiedByName = {"MyConvert","myTransformation1"})
    @Mapping(target = "birthDay", source = "birthDay", dateFormat = "yyyy-MM-dd")
    @Mapping(target = "income", source = "income")
    Person fromUser(User u);


    /**
     * 枚举的装换
     *
     * @param sexEnum
     * @return
     */
    @ValueMappings({
            @ValueMapping(source = "MAN", target = "WOMAN3"),
            @ValueMapping(source = "WOMAN", target = "MAN3"),
            @ValueMapping(source = "SECRECY", target = "SECRECY3")
    })
    Sex3Enum sexConveter(SexEnum sexEnum);


    /**
     * 对象转字符串
     *
     * @param pos
     * @return
     */
//    @Named("myTransformation")// or your custom @Qualifier annotation
//    default String customConveter(Pos pos) {
//        if (null == pos) {
//            return null;
//        }
//        return pos.getName();
//    }

}
